import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { DtailedTabSwitch } from '../../Inventoryplan/mconfig';
import { useCallback } from 'react';
import { ILoadMapTypeAll } from '@/common';

/** 获取差异详情 */
export const detailDiffServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/details?currentUserID=${getUserID()}`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    detailDiffRun: request?.run,
    detailDiffLoading: request?.loading,
    reloadRequest: request.refresh,
  };
};

// 盘点明细
export const DetailedServices = (curRes: Record<string, any>) => {
  const authority: any = DtailedTabSwitch(curRes);
  const request = useRequest(
    (reqParams: any) => {
      const obj = {
        ...authority,
        ...reqParams,
      };
      if (reqParams?.useCompanyID) {
        obj.useCompanyID = reqParams?.useCompanyID
      } else {
        obj.useCompanyID = authority?.useCompanyID
      }
      // 物料管理
      let reqUrl = `${getBaseUrl()}/assetInventory/dataGridDetail/${authority.params}`;
      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, obj);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  const reloadRequest = useCallback((reqParams: any) => {
    request.run({ ...reqParams });
  }, []);

  return {
    DetailPushRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
    Detaildata: request.data,
    getDetailReload: request?.refresh,
    reloadRequest,
  };
};

// 岗位列表
export const positionList = () => {
  // const authority: any = DifferTabSwitch(curRes);
  const request = useRequest(
    (reqParams: any) => {
      let reqUrl = `${getBaseUrl()}/positionController/treeGrid/${reqParams.id}`;
      /***发起请求数据接口**/
      return requestFunComponFiex('GET', reqUrl, reqParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );
  return {
    positiondata: request.data,
    positionRun: request.run,
  };
};

/** 添加盘点差异 */
export const addDiffServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        state: `${getTenantID()}${ILoadMapTypeAll?.ASSET_SUMMARY_STATUS}1000`,
        businessType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_TYPE}1019`,
      };
      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    addDiffRun: request?.run,
    addDiffLoading: request?.loading,
  };
};

/** 编辑盘点差异 */
export const editDiffServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    editDiffRun: request?.run,
    editDiffLoading: request?.loading,
  };
};

/** 手动修改 */
export const manualUpServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        // modifyerID: getUserID(),
        // modifyPositionID: getPosIDs(),
        // modifyDepartmentID: getUserDepID(),
        // modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/manualUp`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    manualUpRun: request?.run,
    manualUpLoading: request?.loading,
  };
};

/** 修改资产 */
export const editDetailServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        // modifyerID: getUserID(),
        // modifyPositionID: getPosIDs(),
        // modifyDepartmentID: getUserDepID(),
        // modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/editDetail`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    editDetailRun: request?.run,
    editDetailLoading: request?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
export const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${getBaseUrl()}/assetInventoryDifference/cancel`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};

/** 领用审批通过后调用 */
export const approvedInventorySerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${getBaseUrl()}/assetInventoryDifference/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedInventoryRun: request?.run,
    approvedInventoryLoading: request?.loading,
  };
};
